﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.Util;
using Com.CMS263.PO;
using Com.CMS263.BLL;

namespace Com.CMS263.Web.manage
{
    public partial class controlaction : System.Web.UI.Page
    {
        ArtTypeBLL atbll = new ArtTypeBLL();
        AdminRoleBLL arbll = new AdminRoleBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            string act = Request["act"];

            int id = string.IsNullOrEmpty(Request["pid"]) ? 0 : int.Parse(Request["pid"]);
            if (act.ToLower().Equals("addcontrol"))
            {
                string ids = GetSonIds(id) + ":" + GetParentIds(id); //+ ":" + GetbrothersIds(id);
                Response.Write(ids);
            }

                //权限修改
            else if (act.ToLower().Equals("savecontrol"))
            {
                int roleId = string.IsNullOrEmpty(Request["roleId"]) ? 0 : int.Parse(Request["roleId"]);
                if (roleId > 0)
                {
                    AdminRole ar = new AdminRole();
                    ar = arbll.GetAdminUserByID(roleId);
                    string power = Request["pow"];
                    ar.rolePower = power;
                    try
                    {
                        arbll.ModifyAdminUser(ar);

                        AdminLoginInfo ali = AdminLoginCookie.GetLoginInfo();
                        ArtTypeBLL atbll = new ArtTypeBLL();
                        OperationLogBLL olbll = new OperationLogBLL();
                        OperationLog ol = new OperationLog();
                        ol.AddDate = DateTime.Now;
                        ol.Ip = StringUtil.GetIP();
                        ol.ModuleName ="修改权限";
                        ol.Contents = "【修改】" + ar.roleName;
                        ol.UserName = ali.UserName;
                        olbll.Add(ol);

                        Response.Write("ok");
                    }
                    catch (Exception)
                    {

                        Response.Write("fail");
                    }
                }
            }
            
        }
        string strids = "";
        public string GetSonIds(int id)
        {
            IList<ArtType> atlist = new List<ArtType>();
            atlist = atbll.GetArtTypeByPid(id);
            if (atlist != null && atlist.Count > 0)
            {
                for (int i = 0; i < atlist.Count; i++)
                {
                    strids += atlist[i].id + ",";
                    GetSonIds(atlist[i].id);
                }
            }
            return strids;
        }
        string strparent = "";
        public string GetParentIds(int id)
        {
            IList<ArtType> atlist = new List<ArtType>();
            ArtType at = new ArtType();
            at = atbll.GetArtTypeById(id);
            if (at.pid != 0)
            {
                strparent += at.pid + ",";
                GetParentIds(at.pid);
            }
            return strparent;
        }
        string brothers = "";
        public string GetbrothersIds(int id)
        {
            IList<ArtType> atlist = new List<ArtType>();
            ArtType at = new ArtType();
            at = atbll.GetArtTypeById(id);
            if (at.pid != 0)
            {
                atlist = atbll.GetArtTypeByPid(at.pid);
                foreach (ArtType att in atlist)
                {
                    brothers += att.id + ",";
                }
                brothers += ":" + at.pid + "'";
            }
            else {
                brothers += "fail,:fail,";
            }
            return brothers;
        }

    }
}
